/**
 * Write a description of class ShockTower here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */

import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class ShockTower extends TOWER
{
    public ShockTower(int x_p, int y_p)
    {
        super(x_p,y_p);
        size = 20;
        radius = 60;
        damage = 30;
        rate = 20;
        count = rate;
        isselected = false;
        drawRange = true;
        upgradelevel = 1;
        cost = 300;
        upgradecost = 20;
    }
    
    public void paint(Graphics g)
    {
        g.drawRect(x,y,size,size);
        g.setColor(java.awt.Color.yellow);
        g.fillRect(x+1,y+1,size-1,size-1);
        g.setColor(java.awt.Color.black);
        for(int i = 1; i <= upgradelevel; i++)
        {
            if(template == false)
            {
                g.drawRect(x-4+i*4, y, 4, 4);
            }
        }
        if(isselected == true)
        {
            g.setColor(Color.green);
            g.drawRect(x-1, y-1, size+2, size+2);
            g.drawRect(x-2, y-2, size+4, size+4);
            g.setColor(Color.black);
        }
        if((isselected == true) && (drawRange == true))
        {
            g.drawOval(x+size/2-radius,y+size/2-radius,radius*2,radius*2);
        }
        if(count == 0)
        {
            g.setColor(java.awt.Color.yellow);
            g.fillOval(x+size/2-radius,y+size/2-radius,radius*2,radius*2);
            g.setColor(java.awt.Color.black);
        }
    }
    public void work(ArrayList<CREEP> creeps)
    {
        if(count == 1)
        {
            if(!creeps.isEmpty())
            {
                for(CREEP c : creeps)
                {
                    int dist = (int)Math.sqrt(Math.pow(c.getxPos()-x+size/2,2)+Math.pow(c.getyPos()-y+size/2,2));
                    if(dist < radius)
                    {
                        c.addDamage(damage);
                    }
                }
                count = 0;
            }
        }
        else
        {
            count = (count==0)?rate:count-1;
        }
    }
    public int sell()
    {
        return (cost+upgradelevel*upgradecost-upgradecost)/2;
    }
    public void upgrade()
    {
        switch(upgradelevel)
        {
            case 1: 
            radius = 65;
            damage = 35;
            upgradelevel++;
            break;
            
            case 2:
            radius = 70;
            damage = 40;
            upgradelevel++;
            break;
            
            case 3:
            radius = 75;
            damage = 45;
            upgradelevel++;
            break;
            
            case 4:
            radius = 85;
            damage = 55;
            upgradelevel++;
            break;
        }
    }
}